<template>
	<view :data-theme="themeStyle">
		<view v-if="weiStartList.length !=0 || show">
			<mescroll-uni @getData="getData" ref="mescroll" :size="10">
				<block slot="list">
					<view class="ns-adv">
						<!-- <image :src="$util.img('upload/default/diy_view/index_seckill_gg.png')" mode="widthFix"></image> -->
						<ns-adv keyword="NS_SECKILL"></ns-adv>
					</view>

					<view class="time-wrap">
						<image :src="$util.img('upload/uniapp/seckill.png')" mode="widthFix"></image>
						<scroll-view :scroll-x="true" class="scroll-wrap" scroll-with-animation
							:scroll-into-view="'a' + seckillId">
							<block v-if="timeList.length>0">
								<view class="time-item" v-for="(item, key) in timeList" :key="key" :id="'a' + item.id"
									@click="ontabtap(item.id, key)">
									<!-- 								v-if="key >= index && dataList.length > 0" -->
									<view class="span font-size-toolbar"
										:class="key == seckillIndex ? 'color-base-text' : ''">
										{{ transformSeckillTime(item.seckill_start_time) }}
									</view>
									<view class="em font-size-activity-tag color-sub"
										:class="seckillIndex == key ? 'color-base-bg active' : ''" v-if="key > index">敬请期待
									</view>
									<view class="em font-size-activity-tag color-sub"
										:class="seckillIndex == key ? 'color-base-bg active' : ''"
										v-if="key == index && !item.isNow">
										即将开始
									</view>
									<view class="em font-size-activity-tag color-sub"
										:class="seckillIndex == key ? 'color-base-bg active' : ''"
										v-if="key == index && item.isNow">
										抢购中
									</view>
									<view class="em font-size-activity-tag color-sub"
										:class="seckillIndex == key ? 'color-base-bg active' : ''" v-if="key < index">已结束
									</view>
								</view>
							</block>
							<view class="time-item" @click="ontabtap()" v-if="weiStartList.length!=0">
								<view class="span font-size-toolbar" :class="isStart ? 'color-base-text' : ''">
									预告
								</view>
								<view class="em font-size-activity-tag color-sub"
									:class="isStart ? 'color-base-bg active' : ''">敬请期待</view>
							</view>
						</scroll-view>
					</view>

					<view class="goods-list single-column" v-if="dataList.length && !isStart"
						@click="isEnd(seckillIndex < index && timeList[index].isNow)">
						<view class="goods-item margin-bottom" v-for="(item, goodsIndex) in dataList" :key="goodsIndex">
							<view class="goods-img" @click="toGoodsDetail(item.id, index, item.goods_stock)">
								<image :src="goodsImg(item.goods_image)" mode="widthFix" @error="imgError(goodsIndex)">
								</image>
								<!-- <image :src="$util.img(item.sku_image, {size: 'mid'})" mode="widthFix" @error="imgError(goodsIndex)"></image> -->
								<view class="color-base-bg goods-tag" v-if="goodsTag(item) != ''">{{ goodsTag(item) }}
								</view>
							</view>
							<view class="info-wrap">
								<view class="name-wrap">
									<view class="goods-name" @click="toGoodsDetail(item.id, index, item.goods_stock)">
										{{ item.goods_name }}</view>
								</view>
								<view class="lineheight-clear">
									<view class="discount-price">
										<text
											class="unit color-base-text font-size-tag">{{ $lang('common.currencySymbol') }}</text>
										<text
											class="price color-base-text font-size-toolbar">{{ item.seckill_price }}</text>
									</view>
								</view>
								<view class="pro-info">
									<view class="delete-price font-size-activity-tag color-tip">
										<text class="unit">{{ $lang('common.currencySymbol') }}</text>
										{{ item.price }}
									</view>
									<view v-if="item.goods_stock > 0" @click="toGoodsDetail(item.id, index)"><button
											type="primary" size="mini">去抢购</button></view>
									<view v-else @click="overgoods"><button type="info"
											style="background: #f2f2f2 !important;" size="mini">已售罄</button></view>
								</view>
							</view>
						</view>
					</view>
					<view class="goods-list single-column" v-if="weiStartList.length>0  && isStart">
						<view class="goods-item margin-bottom" v-for="(item, goodsIndex) in weiStartList" :key="goodsIndex">
							<view class="goods-img" @click="toGoodsDetail(item.id, index, item.goods_stock)">
								<image :src="goodsImg(item.goods_image)" mode="widthFix" @error="imgError(goodsIndex)">
								</image>
								<!-- <image :src="$util.img(item.sku_image, {size: 'mid'})" mode="widthFix" @error="imgError(goodsIndex)"></image> -->
								<view class="color-base-bg goods-tag" v-if="goodsTag(item) != ''">{{ goodsTag(item) }}
								</view>
							</view>
							<view class="info-wrap">
								<view class="name-wrap">
									<view class="goods-name" @click="toGoodsDetail(item.id, index, item.goods_stock)">
										{{ item.goods_name }}<text class="color-base-bg" style="color: #fff;padding:0rpx 10rpx;font-size: 22rpx;">预告</text></view>
								</view>
								<view class="lineheight-clear">
									<view class="discount-price">
										<text
											class="unit color-base-text font-size-tag">{{ $lang('common.currencySymbol') }}</text>
										<text
											class="price color-base-text font-size-toolbar">{{ item.seckill_price }}</text>
									</view>
								</view>
								<view style="font-size: 22rpx;color: #999;margin-top: 10rpx;">活动时间：{{ formatTime(item.start_time) }}</view>
								<view class="pro-info">
									<view class="delete-price font-size-activity-tag color-tip">
										<text class="unit">{{ $lang('common.currencySymbol') }}</text>
										{{ item.price }}
									</view>
									<view v-if="item.goods_stock > 0" @click="toGoodsDetail(item.id, index)"><button
											type="primary" size="mini">去抢购</button></view>
									<view v-else @click="overgoods"><button type="info"
											style="background: #f2f2f2 !important;" size="mini">已售罄</button></view>
								</view>
							</view>
						</view>
					</view>
					<!-- <view v-if="!dataList.length && showEmpty" style="z-index: 1 !important;">
						<ns-empty :fixed="!1" text="该时间段暂无秒杀商品"></ns-empty>
					</view> -->
				</block>
			</mescroll-uni>
		</view>
		<view class="big-empty" v-if="weiStartList.length==0 && dataList.length==0">
			<ns-empty text="当前活动暂未开启"></ns-empty>
		</view>
		<loading-cover ref="loadingCover"></loading-cover>
	</view>
</template>
<script>
	import globalConfig from '@/common/js/golbalConfig.js';
	import list from '../public/js/list.js';
	export default {
		data() {
			return {
				hour: '00',
				minute: '00',
				second: '00',
				ident: false,
				showEmpty: false,
				isVip: '',
				//分享建立上下级所需id
				memberId: 0,
				mpShareData: null, //小程序分享数据
			};
		},
		onLoad(option) {
			this.isVip = option.title
			//获取memberId
			this.$util.getMemberId().then(resolve => {
				this.memberId = resolve;
			}).catch((reject) => {
				// #ifdef MP-WEIXIN
				console.log('reject', reject)
				// #endif
			})
			//小程序分享接收source_member
			if (option.source_member) {
				uni.setStorageSync('source_member', option.source_member);
			}
			// 小程序扫码进入，接收source_member
			if (option.scene) {
				var sceneParams = decodeURIComponent(option.scene);
				sceneParams = sceneParams.split('&');
				if (sceneParams.length) {
					sceneParams.forEach(item => {
						if (item.indexOf('sku_id') != -1) this.skuId = item.split('-')[1];
						if (item.indexOf('m') != -1) uni.setStorageSync('source_member', item.split('-')[1]);
						if (item.indexOf('is_test') != -1) uni.setStorageSync('is_test', 1);
					});
				}
			}
		},

		onShow() {
			if (this.$refs.mescroll) this.$refs.mescroll.refresh();
			// this.$refs.mescroll.refresh()
			// this.$refs.mescroll.getData();
			if (!this.addonIsExit.seckill) {
				this.$util.showToast({
					title: '商家未开启秒杀',
					mask: true,
					duration: 2000
				});
				setTimeout(() => {
					this.$util.redirectTo('/pages/index/index/index', {}, 'redirectTo');
				}, 2000);
				return;
			}

			//记录分享关系
			if (uni.getStorageSync('token') && uni.getStorageSync('source_member')) {
				this.$util.onSourceMember(uni.getStorageSync('source_member'));
			}

			//小程序分享
			// #ifdef MP-WEIXIN
			this.$util.getMpShare().then((res) => {
				this.mpShareData = res;
			});
			// #endif

			this.$langConfig.refresh();
			this.getTimeList();
			// this.getData();
		},
		//分享给好友
		onShareAppMessage() {
			return this.mpShareData.appMessage;
		},
		//分享到朋友圈
		onShareTimeline() {
			return this.mpShareData.timeLine;
		},
		
		mixins: [list, globalConfig],
		onReady() {
			setTimeout(() => {
				const query = uni.createSelectorQuery().in(this);
				query
					.select('.time-wrap')
					.boundingClientRect(data => {
						if (data) this.timeTop = data.top;
					})
					.exec();
			}, 500);

			if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
		}
	};
</script>

<style lang="scss">
	@import '../public/css/list.scss';
</style>